package com.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import com.tank.KeyListener.BulletKeyListener;
import com.tank.KeyListener.TankKeyListener;
import com.tank.config.TankConfig;
import com.tank.model.Bullet;
import com.tank.model.EnemyTank;
import com.tank.model.Explode;
import com.tank.model.HeroTank;
import com.tank.model.Tank;

/**
 * TODO
 *
 * @author chenwb
 * @since 2024/1/21 13:06
 */
public class TankFrame extends Frame {
    private Tank tank = new HeroTank(TankConfig.DEFAULT_TANK_X, TankConfig.DEFAULT_TANK_Y);
    private final List<EnemyTank> enemyTanks = new ArrayList<>();
    private final List<Bullet> bullets = new ArrayList<>();
    private final List<Explode> explodes = new ArrayList<>();
    // 中弹数
    private int killCount = 0;
    public TankFrame() {
        super("Tank");
        setSize(TankConfig.GAME_WIDTH, TankConfig.GAME_HEIGHT);
        setVisible(true);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        addKeyListener(new TankKeyListener(tank));
        addKeyListener(new BulletKeyListener(tank));
    }

    Image offScreenImage = null;
    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(TankConfig.GAME_WIDTH, TankConfig.GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.white);
        gOffScreen.fillRect(0, 0, TankConfig.GAME_WIDTH, TankConfig.GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        // 绘制我方坦克
        tank.paint(g);

        // 绘制我方坦克子弹
        List<Bullet> bullets = tank.getBullets();
        g.drawString("子弹的数量：" + bullets.size(), 50, 50);
        g.drawString("击杀数：" + tank.getKillCount(), 50, 70);
        g.drawString("敌方坦克数量：" + enemyTanks.size(), 50, 90);
        g.drawString("中弹数：" + killCount, 50, 110);

        bullets.forEach(bullet -> bullet.paint(g));

        // 绘制敌方坦克子弹
        for (EnemyTank enemyTank : enemyTanks) {
            enemyTank.getBullets()
                .forEach(bullet -> bullet.paint(g));
        }

        // 绘制敌方坦克
        for (EnemyTank enemyTank : enemyTanks) {
            enemyTank.paint(g);
        }

        // 绘制爆炸
        explodes.forEach(explode -> explode.paint(g));

        // 碰撞判断
        for (Bullet bullet : bullets) {
            for (EnemyTank enemyTank : enemyTanks) {
                if (bullet.getRectangle().intersects(enemyTank.getRectangle())) {
                    Explode explode = new Explode(enemyTank.getX(), enemyTank.getY());
                    explodes.add(explode);
                    bullet.die();
                    enemyTank.die();
                    tank.addKillCount();
                }
                if (tank.getRectangle().intersects(enemyTank.getRectangle())) {
                    Explode explode = new Explode(enemyTank.getX(), enemyTank.getY());
                    explodes.add(explode);
                    tank.die();
                    enemyTank.die();
                    tank.addKillCount();
                }
            }
        }

        // 敌我子弹互相碰撞
        for (Bullet bullet : bullets) {
            for (EnemyTank enemyTank : enemyTanks) {
                enemyTank.getBullets()
                    .forEach(bullet1 -> {
                        if (bullet.getRectangle()
                            .intersects(bullet1.getRectangle())) {
                            bullet.die();
                            bullet1.die();
                        }
                    });
            }
        }

        // 敌方坦克子弹击中我方坦克
        for (EnemyTank enemyTank : enemyTanks) {
            enemyTank.getBullets()
                .forEach(bullet -> {
                    if (bullet.getRectangle()
                        .intersects(tank.getRectangle())) {
                        Explode explode = new Explode(tank.getX(), tank.getY());
                        explodes.add(explode);
                        bullet.die();
                        tank.die();
                        killCount++;
                    }
                });
        }

        // 移除死亡的敌方坦克
        enemyTanks.removeIf(Tank::isAlive);
        for (EnemyTank enemyTank : enemyTanks) {
            enemyTank.getBullets()
                .removeIf(Bullet::isAlive);
        }
        // 移除死亡的我方坦克子弹
        bullets.removeIf(Bullet::isAlive);

        // 移除死亡的爆炸
        explodes.removeIf(Explode::isAlive);
    }

    public void addEnemyTank(EnemyTank enemyTank) {
        this.enemyTanks.add(enemyTank);
    }
}
